package edu.bw.web.role;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.UpdateRoleForm;
import edu.bw.service.RoleService;
import edu.bw.service.impl.RoleServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/role/update")
public class UpdateRoleServlet extends HttpServlet {
    RoleService roleService = new RoleServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{"id":12,"roleName":"测试001","permissions":[3,5],"desc":"测试001","changed":true}
        //1.获取前端发送的参数
        String reqJSON = request.getReader().readLine();
        UpdateRoleForm updateRoleForm = JSONUtil.toBean(reqJSON, UpdateRoleForm.class);

        //2.调用service更新
        Integer rows = roleService.update(updateRoleForm);

        //3.响应数据给前端
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
